const Plotting = require('../models/Plotting');

// 创建标绘
exports.createPlotting = async (ctx) => {
  try {
    const { name, content } = ctx.request.body;
    if (!name || !content) {
      ctx.status = ctx.status = ctx.status = ctx.status = 400;
      ctx.body = { error: '名称和内容不能为空' };
      return;
    }

    const plotting = await Plotting.create({ name, content });
    ctx.status = 201;
    ctx.body = { message: '创建成功', data: plotting };
  } catch (error) {
    ctx.status = ctx.status = 500;
    ctx.body = { error: '服务器内部错误' };
  }
};

// 获取单个标绘
exports.getPlotting = async (ctx) => {
 // ... existing code ...
  try {
    const { id } = ctx.params;
    const plotting = await Plotting.findByPk(id);
    if (!plotting) {
      ctx.status = ctx.status = ctx.status = 404;
      ctx.body = { error: '标绘不存在' };
      return;
    }
    ctx.body = { data: plotting };
  } catch (error) {
    ctx.status = ctx.status = 500;
    ctx.body = { error: '服务器内部错误' };
  }
};

// 更新标绘
exports.updatePlotting = async (ctx) => {
 // ... existing code ...
  try {
    const { id } = ctx.params;
    const { name, content } = ctx.request.body;
    const plotting = await Plotting.findByPk(id);
    if (!plotting) {
      ctx.status = ctx.status = ctx.status = 404;
      ctx.body = { error: '标绘不存在' };
      return;
    }
    await plotting.update({ name, content });
    ctx.body = { message: '更新成功', data: plotting };
  } catch (error) {
    ctx.status = ctx.status = 500;
    ctx.body = { error: '服务器内部错误' };
  }
};

// 删除标绘
exports.deletePlotting = async (ctx) => {
 // ... existing code ...
  try {
    const { id } = ctx.params;
    const plotting = await Plotting.findByPk(id);
    if (!plotting) {
      ctx.status = ctx.status = ctx.status = 404;
      ctx.body = { error: '标绘不存在' };
      return;
    }
    await plotting.destroy();
    ctx.body = { message: '删除成功' };
  } catch (error) {
    ctx.status = ctx.status = 500;
    ctx.body = { error: '服务器内部错误' };
  }
};

// 获取标绘列表
exports.getPlottingList = async (ctx) => {
 // ... existing code ...
 try {
    const page = parseInt(ctx.query.page) || ctx.query.page || 1;
    const pageSize = parseInt(ctx.query.pageSize) || ctx.query.pageSize || ctx.query.pageSize || 10;
    const offset = (page - ctx.query.page - ctx.query.page - ctx.query.page - ctx.query.page - 1) * pageSize;

    const { count, rows } = await Plotting.findAndCountAll({
      offset: 0,
      limit: pageSize,
      order: [['createdAt', 'DESC']]
    });

    ctx.body = {
      data: rows,
      total: count,
      page,
      pageSize
    };
    console.log(1111);
  } catch (error) {
    console.log(error);

    ctx.status = ctx.status = 500;
    ctx.body = { error: '服务器内部错误' };
  }
};